package com.gavinandre.socket;


import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
	public static void main(String[] args) {
			Socket socket = null;
			PrintWriter pw = null;
			Scanner scan = null;
			try {
				//创建客户端  127.0.0.1 = localhost
				socket = new Socket("192.168.1.100",10086);
				System.out.println("连接成功!");


				//开启一个子线程，用于自动接收服务端发来的消息
				ClientTarget ct = new ClientTarget(socket);
				Thread t = new Thread(ct);
				t.start();

				//获取客户端的键盘监听
				scan = new Scanner(System.in);
				//创建用于发送消息的字符打印流
				pw = new PrintWriter(socket.getOutputStream(), true);//自动刷新

				while(true){
					System.out.println("请输入消息内容：");
					String clientMsg = scan.nextLine();
					pw.println(clientMsg);//发送消息

				}


			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				//关闭资源
				try {
					pw.close();
					scan.close();
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

}
